#include "ACUAS/ACUAS.h"
#include "ACUAS/ACUASlcd.h"

//Read data
//Convert data
//Output data to LCD

PROGRAM_INIT
//calibrate sensor 
//display mV value when flat on table
//take values at 45
//take values at 90
//interpolate the rest 
	
	VAR16(x)	//mV value from accelerometer for axis x	
	VAR16(y)	//mV value from accelerometer for axis y
	double ydeg;
	double xdeg;

	ACTIVATE_LCD				//Enable LCD and provide user with initial instructions

	LCD_XY(1,1)
	LCD_TEXT("To Calibrate at 0")
	LCD_XY(1,2)
	LCD_TEXT("Use Joystick Up")

	WAIT_FOR_BUTTON(7){
		CLEAR_LCD				//clear screen and calibrate display to zero degrees for x and y axis	
	}

	ACTIVATE_ADC	//Enable the analog to digital converter

PROGRAM_START
		
		ADC_CHANNEL(1)	//ADC channel 1 used for x axis	conversion
		ADCONVERT_MV(x)

		ADC_CHANNEL(2)	//ADC channel 2 used for y axis conversion
		ADCONVERT_MV(y)

		if (x <= 1155.)
		{
			xdeg = ((45. - 0.) / (1155. - 859.)) * (x - 859.);
		}
		else
		{
			xdeg = ((90. - 45.) / (1450. - 1155.)) * (x - 1155.);
		}

/*		if (y <= 537.)
		{
			ydeg = ((45. - 0.) / (537. - 898.)) * (y - 537.);
		}
		else if (y > 537.)
		{
			ydeg = ((45.- 90.) / (361. - 537.)) * (y - 361.);
		}
*/

//		LCD_XY(1,4)
//		LCD_TEXT(" ")
		
//		LCD_XY(2,4)
//		LCD_TEXT(" ")				

		LCD_XY(1,1)
		LCD_NUMBER(xdeg)

		LCD_XY(1,2)
		LCD_NUMBER(ydeg) 

/*		LCD_XY(1,1)
		LCD_NUMBER(x)

		LCD_XY(1,2)
		LCD_NUMBER(y) 
*/			

PROGRAM_END
